/**
 * 
 */
package nl.samenloop.bigscreen.sequence;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nl.samenloop.bigscreen.Constants;
import nl.samenloop.bigscreen.communication.Result;
import nl.samenloop.bigscreen.communication.StreamingRSSReader;
import nl.samenloop.bigscreen.model.Message;

/**
 * @author Bram Wiekens
 * 
 */
public class TwitterSequence extends Sequence {

	JPanel firstPanel;
	JPanel secondPanel;

	JPanel current;
	JPanel next;

	public TwitterSequence(DrawingTarget target) {
		super(target, Constants.TWITTER_TIME);
	}

	@Override
	public void start() {
		Container c = target.getCenterContainer();

		createPanels(c);

		start(getTitle(Constants.TWITTER));

	}

	private void createPanels(Container c) {
		firstPanel = new JPanel();
		firstPanel.setLayout(null);
		firstPanel.setBackground(Constants.PURPLE);
		firstPanel.setBounds(0, 0, c.getWidth(), c.getHeight());
		firstPanel.setLocation(-1 * firstPanel.getWidth(), 0);

		secondPanel = new JPanel();
		secondPanel.setLayout(null);
		secondPanel.setBackground(Constants.PURPLE);
		secondPanel.setBounds(0, 0, c.getWidth(), c.getHeight());
		secondPanel.setLocation(-1 * secondPanel.getWidth(), 0);

		c.add(firstPanel);
		c.add(secondPanel);
	}

	public void setMessages(final List<Message> messages) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				for (Message message : messages) {
					try {
						setNext();

						createAndMoveBlock(message);

						Thread.sleep(6500);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}).start();

	}

	private void setNext() {
		if (next == null && current == null) {
			next = firstPanel;
			current = secondPanel;
		} else {
			JPanel temp = current;
			current = next;
			next = temp;
			
		}

	}

	protected JComponent createLabel(String text, int size) {
		String msg = String.format("<html><div WIDTH=%d>%s</div><html>", next.getWidth(), text);

		JLabel label = new JLabel(msg);
		label.setDoubleBuffered(true);

		label.setFont(new Font("Droid Sans", Font.BOLD, size));
		label.setForeground(Color.white);

		return label;
	}

	public void createAndMoveBlock(Message message) {
		next.removeAll();

		String msg = message.getTitle();

		JComponent label = createLabel(msg, 30);
		label.setBounds(10, 0, next.getWidth(), next.getHeight());
		next.add(label);

		next.add(label);

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				if (current != null) {
					moveOutBlock();
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				for (int i = (-1 * next.getBounds().width); i < 10; i += 40) {
					next.setLocation(i, 0);

					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});

		t.start();

	}

	public void moveOutBlock() {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < 500; i++) {

					int position = (i * i * i) / 10;

					Rectangle bounds = current.getBounds();
					current.setLocation(bounds.x + position, bounds.y);

					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				// current.removeAll();
				current.setLocation(-1 * current.getWidth(), current.getHeight());
			}

		});

		t.start();
	}

	@Override
	public void prepare() {
		StreamingRSSReader reader = new StreamingRSSReader(Constants.getProperty(Constants.TIMELINE_URL));

		reader.setResultHandler(new Result<ArrayList<Message>>() {

			@Override
			public void onSucces(ArrayList<Message> result) {
				setMessages(result.subList(0, 6));
			}

			@Override
			public void onFailure(Throwable e) {
				e.printStackTrace();

			}
		});

		Thread t = new Thread(reader);
		t.start();
	}

}
